package com.demo.controller;

import com.demo.dao.Result;

import com.demo.pojo.DTO.DTORoleMenu;
import com.demo.pojo.PO.User;
import com.demo.service.system.UserService;
import com.demo.util.JwtUtil;
import com.demo.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;


    /*
    用户登录
     */
    @PostMapping
    public Result login(String username, String password) {
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        String md5String = Md5Util.getMD5String(password);
        String newPwd = user.getPassword();
        if (!md5String.equals(newPwd)) {
            return Result.error("密码错误");
        }
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("username", username);
        String token = JwtUtil.genToken(claims);
        return Result.success(token);
    }

    // 获取用户的角色和权限
    @GetMapping
    public Result getRoleAndMenuByUserId(Integer userId) {
        List<DTORoleMenu> UserRAndM = userService.getRAndMById(userId);
        System.out.println(UserRAndM);
        return Result.success(UserRAndM);
    }
}
